#include <iostream>
#include <cstdlib>
#include <memory>
#include "Socket.hpp"
#include "Reactor.hpp"
#include "Listener.hpp"
#include "InetAddr.hpp"
#include "HandleConnection.hpp"

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " port" << std::endl;
        return 0;
    }
    uint16_t port = atoi(argv[1]);
    InetAddr addr("0.0.0.0", port);
    Listenner listener(port);                                      // 连接管理器
    HandleConnection handle;                                       // IO处理器
    std::unique_ptr<Reactor> server = std::make_unique<Reactor>(); // 事件派发器
    server->SetOneConnection(std::bind(&Listenner::Accepter, &listener, std::placeholders::_1));
    server->SetNormalConnection(
        std::bind(&HandleConnection::HandleRecv, &handle, std::placeholders::_1),
        std::bind(&HandleConnection::HandleSend, &handle, std::placeholders::_1),
        std::bind(&HandleConnection::HandleError, &handle, std::placeholders::_1)
    );
    server->AddConnection(listener.Listenfd(), EPOLLIN | EPOLLET, addr, LISTENLOCKET);
    server->Distributor();
    return 0;
}